﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
using INTEX.DataBase.Tools;
using System.IO;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        private void YGRAB50GB20(DataSet db)
        {

            try
            {
                string value = "";
                DataRow row = null;
                int x = 1, y = 1;
                x = 3; y = 5;
                string str1 = "", str2 = "", str3 = "";
                DataRow dwOrder = db.Tables["ABPOS"].Rows[0];
                DataRow vk = db.Tables["VKPOS"].Rows[0];
                string HMKRS = dwOrder["ABPAUFKRS"].ToString().Trim();
                string HMLFD = dwOrder["ABPAUFLFD"].ToString().Trim();
                string HMPOS = dwOrder["ABPAUFPOS"].ToString().Trim();
                string APKSPEZIF = GetGB2312Value(dwOrder["APKSPEZIF"].ToString().Trim());
                double ABPSTKLG = Convert.ToDouble(dwOrder["ABPSTKLG"].ToString()) / 10000000;
                double ABPSOLLMENGE7 = Convert.ToDouble(dwOrder["ABPSOLLMENGE7"].ToString()) / 100;
                double ABPSOLLMENGE5 = Convert.ToDouble(dwOrder["ABPSOLLMENGE5"].ToString()) / 100;
                if (ABPSTKLG == 0)
                    ABPSTKLG = 1;
                if (ABPSOLLMENGE7 == 0)
                    ABPSOLLMENGE7 = ABPSOLLMENGE5;
                double TarGetRatio = Math.Round(ABPSOLLMENGE5 * ABPSTKLG / ABPSOLLMENGE7, 2);
                if (TarGetRatio == 0)
                    TarGetRatio = 1;
                value = HMKRS + "-" + Helper.GetSubRollNum(HMLFD) + "-" + Helper.GetSubRollNum(HMPOS);
                exAgent.SetCellValue(3, 5, value);
                string 生产单条码 = "*" + HMKRS + HMLFD + HMPOS + "01*";
                exAgent.SetCellValue(2, 22, 生产单条码);
                exAgent.SetCellValue(1, 2, "混棉联系单(" + value + ")");
                x = 3; y = 30;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                exAgent.SetCellValue(2, 37, str3);
                int positions = 0;
                if (!db.Tables.Contains("ABAUFM"))
                {
                    return;
                }
                string ABPART, ABPVAR, ABPFARBE;

                ABPART = dwOrder["ABPART"].ToString();
                ABPVAR = dwOrder["ABPVAR"].ToString();
                ABPFARBE = dwOrder["ABPFARBE"].ToString();

                //exAgent.SetCellValue(3, 14, ABPART.Trim());
                exAgent.SetCellValue(4, 5, APKSPEZIF.Trim());
                exAgent.SetCellValue(4, 14, ABPFARBE.Trim());

                var query = from DataRow dwRow in db.Tables["GSCOLOR"].AsEnumerable()
                            where dwRow["GSCART"].ToString() == ABPART
                            && dwRow["GSCFARBE"].ToString() == ABPFARBE
                            select dwRow;
                //foreach (DataRow dwRow in query)
                //{
                //    exAgent.SetCellValue(5, 14, GetGB2312Value(dwRow["GSCBEZ"].ToString()));
                //}
                string gpm = "";
                string MU = "";
                string TY = "";
                double batchqty = 0;
                double qty = 0;
                query = from DataRow dwRow in db.Tables["TBABA"].AsEnumerable()
                        where
                         dwRow["ABAAUFKRS"].ToString() == HMKRS
                        && dwRow["ABAAUFLFD"].ToString() == HMLFD
                        && dwRow["ABAAUFPOS"].ToString() == HMPOS
                        select dwRow;

                int idx = 0;
                int idxmu = 0;
                exAgent.SetCellValue(5, 5, TarGetRatio);
                string 细纱单 = "";
                string 条码 = "";
                int mustart = 20;
                foreach (DataRow dwRow in query)
                {
                    MU = dwRow["ABAAUFMSL"].ToString();
                    double ABARESMG = Convert.ToDouble(dwRow["ABARESMG"].ToString()) * 0.001;
                    double ABARSTSGEW = Convert.ToDouble(dwRow["ABARSTSGEW"].ToString()) * 0.001;

                    if (ABARESMG == 0 && ABARSTSGEW == 0) continue;
                    string mu = Helper.GetSDDAT(db, "0015", MU, "V");
                   // exAgent.SetCellValue(mustart + idx, 1, "定量");
                    //exAgent.SetCellValue(mustart + idx, 5, mu.Replace("粗纱定量", ""));
                    条码 = "*" + dwRow["ABAAUFKRS"].ToString() + dwRow["ABAAUFLFD"].ToString() + dwRow["ABAAUFPOS"].ToString() + Helper.Right(dwRow["ABAAPOS"].ToString(), 2) + "*";
                    exAgent.SetCellValue(mustart + idx, 11, 条码);
                    细纱单 = dwRow["ABAVKAUFKRS"].ToString() + "-" + dwRow["ABAVKAUFLFD"].ToString() + "-" + dwRow["ABAVKAUFPOS"].ToString();
                    exAgent.SetCellValue(mustart + idx, 31, 细纱单);
                    exAgent.SetCellValue(mustart + idx, 39, ABARESMG);
                    //exAgent.SetCellValue(6 + idx, 11, "重量");
                    if (ABARESMG > 0)
                    {
                        //exAgent.SetCellValue(6 + idx, 14, ABPSOLLMENGE7);
                    }
                    else
                    {
                        //exAgent.SetCellValue(6 + idx, 14, ABPSOLLMENGE7);
                    }
                    idx++;
                }
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = HMKRS;
                dwData["ABAAUFLFD"] = HMLFD;
                dwData["ABAAUFPOS"] = HMPOS;
                DataSet SResult = INTEXDBC.QueryABAUFM(dwData, 11);
                DataTable TBMU = SResult.Tables[0].Copy();
                query = from DataRow dwRow in TBMU.AsEnumerable()
                        where dwRow["ABAVKAUFTYP"].ToString() == "K"
                        select dwRow;
                idx = 0;
                idxmu = 0;
                string 强力 = "", 捻度 = "", 对色标准 = "";
                string package = "", kdcolor = "", VKPCOLOR = "", YARNNE = "", YARNNETOT;
                Int32 V_YARNNE = 0;
                string processed = "";
                decimal D_YARNNE = 0, D_YARNNEMAX = 0;
                foreach (DataRow dwRow in query)
                {
                    double ABARESMG = Convert.ToDouble(dwRow["ABARESMG"].ToString()) * 0.001;
                    YARNNETOT = dwRow["YARNNE"].ToString().Trim();
                    V_YARNNE = YARNNETOT.IndexOf("S", 0);
                    YARNNE = YARNNETOT.Substring(0, V_YARNNE);
                    D_YARNNE = System.Convert.ToDecimal(YARNNE);
                    var qVK = from DataRow VKP in db.Tables["VKPOS"].AsEnumerable()
                              where VKP["VKPAUFKRS"].ToString() + "-" + VKP["VKPAUFLFD"].ToString() + "-" + VKP["VKPAUFPOS"].ToString() == dwRow["ORDERNO"].ToString()
                              select VKP;
                    foreach (DataRow VKP in qVK)
                    {
                        string ORDERNO = VKP["VKPAUFKRS"].ToString().Trim() + "-" + Helper.GetSubRollNum(VKP["VKPAUFLFD"].ToString()) + "-" + Helper.GetSubRollNum(VKP["VKPAUFPOS"].ToString());
                        if (processed.Contains(ORDERNO + ";")) continue;
                        processed += ORDERNO + ";";
                        string VKPKDARTNR = VKP["VKPKDARTNR"].ToString().Substring(0, 14);
                        VKPCOLOR = GetGB2312Value(VKP["VKPKDFARBNR"].ToString().Trim());
                        double VKPMENGE1 = Convert.ToDouble(VKP["VKPMENGE1"].ToString()) / 100;
                        if (VKPKDARTNR.Trim() == "")
                        {
                            ORDERNO = GetGB2312Value(VKP["KDKUNDNA"].ToString().Trim()) + "(" + ORDERNO + ")";
                        }
                        else
                        {
                            ORDERNO = GetGB2312Value(VKPKDARTNR.Trim()) + "(" + ORDERNO + ")";
                        }
                        if (强力 == "")
                        {
                            强力 = GetGB2312Value(VKP["STRENGTH"].ToString());
                        }
                        if (捻度 == "")
                        {
                            捻度 = GetGB2312Value(VKP["TWIST"].ToString());
                        }
                        if (对色标准 == "")
                        {
                            对色标准 = GetGB2312Value(VKP["COLORSTD"].ToString());
                        }
                        if (package == "")
                        {
                            var que = from DataRow dwQUE in db.Tables["SDDAT"].AsEnumerable()
                                      where dwQUE["SDKEY"].ToString() == "00" + VKP["VKPVERPART"].ToString() + "                  V"
                                     && dwQUE["SDSA"].ToString() == "0209"
                                      select dwQUE;
                            foreach (DataRow dwQUE in que)
                            {
                                package = GetGB2312Value(dwQUE["SDDATEN"].ToString().Substring(1, 20).Trim());
                            }
                        }
                        exAgent.SetCellValue(4 + idxmu, 19, ORDERNO);
                        exAgent.SetCellValue(4 + idxmu, 27, dwRow["ABAKDPARTIE"].ToString());
                        dwData["GPSPPART"] = dwRow["ABAKDPARTIE"].ToString();
                        DataSet sR = INTEXDBC.QueryABAUFM(dwData, 13);
                        var SO = from DataRow dwSO in sR.Tables[0].AsEnumerable()
                                 select dwSO;
                        double SOQTY = 0.0;
                        foreach (DataRow dwSO in SO)
                        {
                            SOQTY = Convert.ToDouble(dwSO["QTY"].ToString().Trim());
                        }
                        if (SOQTY < VKPMENGE1)
                        {
                            exAgent.SetCellValue(4 + idxmu, 31, Convert.ToDouble(SOQTY));
                        }
                        else
                        {
                            exAgent.SetCellValue(4 + idxmu, 31, Convert.ToDouble(VKP["VKPMENGE1"].ToString()) / 100);
                        }
                        /*if (ABPSOLLMENGE7 < VKPMENGE1)
                        {
                            exAgent.SetCellValue(4 + idxmu, 31, Convert.ToDouble(dwOrder["ABPSOLLMENGE7"].ToString()) / 100);
                        }
                        else
                        {
                            exAgent.SetCellValue(4 + idxmu, 31, Convert.ToDouble(VKP["VKPMENGE1"].ToString()) / 100);
                        }*/

                        // exAgent.SetCellValue(4 + idxmu, 31, Convert.ToDouble(dwOrder["ABPSOLLMENGE7"].ToString()) / 100);
                        if (VKP["VKPWUTERM"].ToString() == VKP["VKPLIEFTERM"].ToString())
                        {
                            exAgent.SetCellValue(4 + idxmu, 34, VKP["VKPLIEFTERM"].ToString());
                        }
                        else
                        {
                            exAgent.SetCellValue(4 + idxmu, 34, VKP["VKPWUTERM"].ToString()+"-"+VKP["VKPLIEFTERM"].ToString().Substring(4));
                        }
                        exAgent.SetCellValue(4 + idxmu, 39, VKP["YARNTYP"].ToString().Trim() + dwRow["YARNNE"].ToString());
                        if (D_YARNNEMAX < D_YARNNE)
                        {
                            D_YARNNEMAX = D_YARNNE;
                            exAgent.SetCellValue(3, 17, VKP["YARNTYP"].ToString().Trim() + YARNNETOT);
                        }
                    }
                    if (kdcolor == "")
                    {
                        var kdc = from DataRow dwKDC in db.Tables["SDDAT"].AsEnumerable()
                                  where dwKDC["SDKEY"].ToString() == "00" + ABPFARBE + "           V"
                                 && dwKDC["SDSA"].ToString() == "0014"
                                  select dwKDC;
                        foreach (DataRow dwKDC in kdc)
                        {
                            kdcolor = GetGB2312Value(dwKDC["SDDATEN"].ToString().Substring(51, 10).Trim());
                            exAgent.SetCellValue(5, 14, kdcolor);
                            exAgent.SetCellValue(6, 14, GetGB2312Value(dwKDC["SDDATEN"].ToString().Substring(1, 28).Trim()));
                        }
                    }
                    idxmu++;
                    /*if (!kdcolor.Contains(VKPCOLOR))
                    {
                        kdcolor = kdcolor + ";" + VKPCOLOR;
                    }*/

                }
                if (idxmu == 1)
                {
                    exAgent.SetCellValue(7, 5, 对色标准);
                    exAgent.SetCellValue(7, 14, 强力);
                    exAgent.SetCellValue(8, 14, 捻度);
                    exAgent.SetCellValue(8, 5, package);
                    /*if (kdcolor != "")
                    {
                        kdcolor = kdcolor.Substring(1);
                    }*/

                }
               int cgd = db.Tables["GAAUF"].Rows.Count-1;

                query = from DataRow dwRow in db.Tables["ABAUFM"].AsEnumerable()
                        where dwRow["ABAVKAUFKRS"].ToString() == HMKRS
                            && dwRow["ABAVKAUFLFD"].ToString() == HMLFD
                            && dwRow["ABAVKAUFPOS"].ToString() == HMPOS
                        select dwRow;
                int count = query.Count();
                /*if (count != cgd)
                {
                    exAgent.SetCellValue(1, 2, "分盘单原料个数不正确，请计划人员核对");

                    throw new Exception("分盘单原料个数不正确，请计划人员核对," + "GAAUF个数：" + cgd + "FP个数：" + count);
                }
                cgd = 0; */ 

                double kg = 0, kg3 = 0;
                x = 0;
                string ndate = "";
                int nRow = 2;
                int xStart = 36;
                idx = 0;
                DataRow dwGAAUF = null;
                Dictionary<int, string> EachBatch = new Dictionary<int, string>();
                foreach (DataRow dwRow in query)
                {
                    ABPART = dwRow["ABPART"].ToString();
                    ABPVAR = dwRow["ABPVAR"].ToString();
                    ABPFARBE = dwRow["ABPFARBE"].ToString();
                    string UPOS = dwRow["ABAVKUPOS"].ToString();
                    kg = Math.Round(System.Convert.ToDouble(dwRow["ABARESMG"].ToString()) * 0.001, 2);
                    string fpkrs, fplfd, fppos, apos;

                    fpkrs = dwRow["ABAAUFKRS"].ToString();
                    fplfd = dwRow["ABAAUFLFD"].ToString();
                    fppos = dwRow["ABAAUFPOS"].ToString();
                    apos = dwRow["ABAAPOS"].ToString();
                    var qGA = from DataRow ga in db.Tables["GAAUF"].AsEnumerable()
                              where ga["GAAUFKRS"].ToString() == HMKRS
                              && ga["GAAUFLFD"].ToString() == HMLFD
                              && ga["GAAUFPOS"].ToString() == HMPOS
                              && ga["GAPOS"].ToString() == UPOS
                              && ga["GAART"].ToString() == ABPART
                              && ga["GAVAR"].ToString() == ABPVAR
                              && ga["GAFARBE"].ToString() == ABPFARBE
                              select ga;
                    foreach (DataRow ga in qGA)
                    {
                        dwGAAUF = ga;
                    }
                    #region=====BATCHPATTERN====
                    double MQTY = 0;
                    double BATCHES = 1;
                    double BATCHEQTY = 0;
                    string BATCHPATTERN = "";
                    string eachbath = "";
                    var qMU = from MUrow in db.Tables["ABAUFM"].AsEnumerable()
                              group MUrow by new
                              {
                                  KRS = MUrow["ABAAUFKRS"].ToString(),
                                  LFD = MUrow["ABAAUFLFD"].ToString(),
                                  APOS = MUrow["ABAAPOS"].ToString(),
                              }
                                  into g
                              select new
                              {
                                  KEY = g.Key,
                                  BATCHES = g.Max(XD => Convert.ToDouble(XD["ABASANZSTK"].ToString())),
                                  MQTY = g.Sum(XD => Convert.ToDouble(XD["ABARESMG"].ToString()) / 1000),
                              };
                    foreach (var mu in qMU)
                    {
                        if (mu.KEY.APOS == apos)
                        {
                            MQTY = mu.MQTY;
                            BATCHES = mu.BATCHES;
                            BATCHEQTY = Math.Round(MQTY / BATCHES, 1);
                            eachbath = BATCHEQTY.ToString() + "x" + BATCHES + Environment.NewLine + "(" + Math.Round(MQTY).ToString() + ")";
                        }
                        if (idx == 0)
                        {
                            BATCHPATTERN += Convert.ToInt16(mu.KEY.APOS) - 1 + ":" + mu.MQTY + ";";
                            qty += mu.MQTY;
                        }
                    }
                    if (idx == 0)
                    {
                        BATCHPATTERN += "***";
                        BATCHPATTERN = BATCHPATTERN.Replace(";***", "");
                        exAgent.SetCellValue(34, 29, BATCHPATTERN);
                        exAgent.SetCellValue(34, 34, qty);
                    }
                    if (MQTY == 0)
                        MQTY = 1;
                    exAgent.SetCellValue(xStart + idx * nRow, 40, eachbath);
                    #endregion
                    string GSGARNBEZ = dwGAAUF["GSGARNBEZ"].ToString();
                    MU = dwRow["ABAAUFMSL"].ToString();
                    double GABASMG2 = Convert.ToDouble(dwGAAUF["GABASMG2"].ToString()) / 1000;
                    //MU = Helper.GetSDDAT(db, "0015", MU, "V");
                    var qFP = from DataRow FP in db.Tables["ZUTEI_EXT"].AsEnumerable()
                              where FP["ZUAUFKRS"].ToString() == fpkrs
                                 & FP["ZUAUFLFD"].ToString() == fplfd
                                & FP["ZUAUFPOS"].ToString() == fppos
                              select FP;
                    foreach (DataRow FP in qFP)
                    {
                        TY = GetGB2312Value(FP["ZUSPPART"].ToString().Trim());
                    }
                    var qu = from DataRow dwQU in db.Tables["SDDAT"].AsEnumerable()
                             where dwQU["SDKEY"].ToString() == "00" + MU + "                   V"
                             && dwQU["SDSA"].ToString() == "0015"
                             select dwQU;
                    foreach (DataRow dwQU in qu)
                    {
                        MU = GetGB2312Value(dwQU["SDDATEN"].ToString().Substring(1, 20).Trim());
                    }
                    string code = "*" + fpkrs + fplfd + fppos + Helper.Right(apos, 2) + "*";
                    exAgent.SetCellValue(xStart + idx * nRow, 1, Convert.ToInt16(apos) - 1);
                    exAgent.SetCellValue(xStart + idx * nRow, 3, code);
                    exAgent.SetCellValue(xStart + idx * nRow, 17, MU);
                    exAgent.SetCellValue(xStart + idx * nRow, 19, ABPART.Trim());// + ABPVAR.Trim());
                    //exAgent.SetCellValue(xStart + idx * nRow, 19, GetGB2312Value(GSGARNBEZ).Trim());// + ABPVAR.Trim());
                    exAgent.SetCellValue(xStart + idx * nRow + 1, 19, GetGB2312Value(ABPFARBE.Trim()));
                    if (MU != "特殊原料")
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 24, GetGB2312Value(dwGAAUF["GABEM"].ToString().Trim()));
                    }
                    else
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 24, GetGB2312Value(dwGAAUF["GABEM"].ToString().Trim()) + "/" + TY);
                    }
                    exAgent.SetCellValue(xStart + idx * nRow, 35, 0.001 * Convert.ToDouble(dwRow["ABASSTKGEW"].ToString()));//盘重
                    exAgent.SetCellValue(xStart + idx * nRow, 37, dwRow["ABASANZSTK"].ToString().Trim());//盘数 
                    exAgent.SetCellValue(xStart + idx * nRow, 38, kg);
                    double GASP1 = Convert.ToDouble(dwGAAUF["GASP1"].ToString()) / 1000;
                    if (GASP1 == 0)
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 33, Math.Round(GABASMG2 / ABPSTKLG, 2) + "%");
                    }
                    else
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 33, Math.Round(GASP1, 2) + "%");
                    }

                    #region=====Alloction=======
                    Dictionary<string, double> ListZu = new Dictionary<string, double>();
                    if (db.Tables.Contains("ZUTEI_EXT"))
                    {
                        var qzu = from rows in db.Tables["ZUTEI_EXT"].AsEnumerable()
                                  where rows["ZUAUFKRS"].ToString() == fpkrs
                                     & rows["ZUAUFLFD"].ToString() == fplfd
                                    & rows["ZUAUFPOS"].ToString() == fppos
                                  //& rows["ZUPOS"].ToString() == UPOS
                                  select rows;

                        double ZUEINTKG = 0;
                        double QTYZU = 0;
                        foreach (DataRow rw in qzu)
                        {
                            string sppartie = rw["ZUSPPART"].ToString().Trim();
                            GSGARNBEZ = GetGB2312Value(rw["GSGARNBEZ"].ToString().Trim());
                            exAgent.SetCellValue(xStart + idx * nRow, 19, GSGARNBEZ);// + ABPVAR.Trim());
                            string LPBEZ = GetGB2312Value(rw["LPBEZ"].ToString().Trim());
                            ZUEINTKG = Convert.ToDouble(rw["ZUEINTKG"].ToString()) / 1000;
                            if (ListZu.ContainsKey(LPBEZ))
                            {
                                QTYZU = ListZu[LPBEZ];
                                QTYZU += ZUEINTKG;
                            }
                            else
                            {
                                QTYZU = ZUEINTKG;
                                ListZu.Add(LPBEZ, QTYZU);
                            }
                        }
                    }
                    string allocation = "";
                    int stkcount = ListZu.Count;
                    foreach (KeyValuePair<string, double> kp in ListZu)
                    {
                        if (stkcount > 1)
                        {
                            allocation += kp.Key + "(" + kp.Value.ToString() + ")";
                        }
                        else
                        {
                            allocation += kp.Key;
                        }
                    }

                    exAgent.SetCellValue(xStart + idx * nRow, 27, allocation);
                    #endregion
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(xStart, xStart + 1, xStart + idx * nRow, xStart + idx * nRow + 1);
                    }
                }

                string REMARK = "";
                foreach (DataRow rwtxt in db.Tables["TXTKON"].Rows)
                {

                    REMARK += Helper.GetGB2312Value(rwtxt["TXTKTEXT"].ToString().Trim() + ",");
                }
                int leng = REMARK.Length;
                if (leng > 0)
                {
                    REMARK += "****";
                    REMARK = REMARK.Replace(",****", "");
                } 
                exAgent.SetCellValue(xStart + idx * nRow + 1, 1, "备注:" + REMARK);
                idx++;

                var VKPOS = from DataRow dR in TBMU.AsEnumerable()
                            where dR["ABAVKAUFTYP"].ToString() == "K"
                            select dR;
                string remark = "", STR1 = "", STR2 = "", STR3 = "", STR4 = "", STR5 = "", vkkorder = "", overshort = "",vkcolor="";
                foreach (DataRow dR in VKPOS)
                {
                    string PARTIE = dR["ABAKDPARTIE"].ToString().Trim();
                    var qVK = from DataRow VKP in db.Tables["VKPOS"].AsEnumerable()
                              where VKP["VKPAUFKRS"].ToString() + "-" + VKP["VKPAUFLFD"].ToString() + "-" + VKP["VKPAUFPOS"].ToString() == dR["ORDERNO"].ToString()
                              select VKP;
                    foreach (DataRow VKP in qVK)
                    {
                        string ORDNO = VKP["VKPAUFKRS"].ToString().Trim() + "-" + Helper.GetSubRollNum(VKP["VKPAUFLFD"].ToString()) + "-" + Helper.GetSubRollNum(VKP["VKPAUFPOS"].ToString());
                        string CO = VKP["VKPKDARTNR"].ToString().Substring(0, 14);
                        string tkd = GetGB2312Value(VKP["VKPKDARTNR"].ToString().Substring(13, 10).Trim());
                        vkkorder = GetGB2312Value(VKP["VKKORDER"].ToString().Trim());
                        STR1 = GetGB2312Value(VKP["VKPKDFARBNR"].ToString().Trim());
                        if (CO.Trim() == "")
                        {
                            ORDNO = GetGB2312Value(VKP["KDKUNDNA"].ToString().Trim()) + "(" + ORDNO + ")";
                        }
                        else
                        {
                            ORDNO = GetGB2312Value(CO.Trim()) + "(" + ORDNO + ")";
                        }
                        STR2 = GetGB2312Value(VKP["STRENGTH"].ToString().Trim());

                        STR3 = GetGB2312Value(VKP["TWIST"].ToString().Trim());

                        STR4 = GetGB2312Value(VKP["COLORSTD"].ToString().Trim());

                        overshort = GetGB2312Value(VKP["OVERSHORT"].ToString().Trim());

                        vkcolor = GetGB2312Value(VKP["VKPFARBE"].ToString().Trim());
                        var que = from DataRow dwQUE in db.Tables["SDDAT"].AsEnumerable()
                                  where dwQUE["SDKEY"].ToString() == "00" + VKP["VKPVERPART"].ToString() + "                  V"
                                 && dwQUE["SDSA"].ToString() == "0209"
                                  select dwQUE;
                        foreach (DataRow dwQUE in que)
                        {
                            STR5 = GetGB2312Value(dwQUE["SDDATEN"].ToString().Substring(1, 20).Trim());
                        }

                        string YARN = VKP["YARNTYP"].ToString().Trim() + dR["YARNNE"].ToString().Trim();
                        remark = ORDNO + ",批号：" + PARTIE + "，纱支：" + YARN + "，客户色号：" + STR1 + "，强力：" + STR2
                            + "，用途：" + tkd + "，捻度：" + STR3 + "，对色：" + STR4;
                        exAgent.SetCellValue(2, 2, "新雅色号：" + vkcolor);
                        exAgent.SetCellValue(xStart + idx * nRow, 1, remark);

                        DataRow dwTXTIND = INTEXDB.ICC_QUERYTB.NewRow();
                        dwTXTIND["TXTIMANDANT"] = app.Mandant;
                        dwTXTIND["TXTITXTART"] = "VKT";
                        dwTXTIND["TXTISIND"] = VKP["VKPAUFKRS"].ToString() + VKP["VKPAUFLFD"].ToString() + VKP["VKPAUFPOS"].ToString() + "IVDI";
                        DataSet dsTXT = INTEXDBC.QueryTXTKON(dwTXTIND, 2);
                        var TXTKON = from DataRow drTXT in dsTXT.Tables[0].AsEnumerable()
                                     select drTXT;
                        string TXT = "";
                        foreach (DataRow drTXTKON in TXTKON)
                        {
                            string TXTK = GetGB2312Value(drTXTKON["TXTKTEXT"].ToString().Trim());
                            TXT = TXT + "/" + TXTK;

                        }
                        if (TXT != "")
                        {
                            TXT = TXT.Substring(1);
                        }
                        exAgent.SetCellValue(xStart + idx * nRow + 1, 1, TXT + "，包装：" + STR5 + "，重量标准：" + vkkorder + "，订单短溢装：" + overshort);
                        DataRow dwtxt = INTEXDB.ICC_QUERYTB.NewRow();
                        dwtxt["TXTIMANDANT"] = app.Mandant;
                        dwtxt["TXTITXTART"] = "VFB";
                        dwtxt["TXTISIND"] = VKP["VKPAUFKRS"].ToString() + VKP["VKPAUFLFD"].ToString() + VKP["VKPAUFPOS"].ToString();
                        DataSet dstxt = INTEXDBC.QueryTXTKON(dwtxt, 2);
                        var txtkon = from DataRow drtxt in dstxt.Tables[0].AsEnumerable()
                                     select drtxt;
                        string txt = "";
                        foreach (DataRow drtxtkon in txtkon)
                        {
                            string txtk = GetGB2312Value(drtxtkon["TXTKTEXT"].ToString().Trim());
                            txt = txt + "/" + txtk;

                        }
                        if (txt != "")
                        {
                            txt = txt.Substring(1);
                        }
                        exAgent.SetCellValue(xStart + idx * nRow -1, 1,"备注:" + REMARK+";"+txt);
                        idx++;
                    }

                }
                MergeSameBATCH(xStart, count, nRow, 40);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void MergeSameBATCH(int xstart, int count, int nRow, int ystart)
        {
            int x1, y1, x2, y2;
            string str = "";
            string str1;
            y1 = ystart;
            y2 = ystart;
            x1 = xstart;
            x2 = xstart;
            for (int i = 0; i <= count; i++)
            {
                str1 = (string)exAgent.GetCellValue(xstart + i * nRow, ystart);
                if (str == str1)
                {
                    x2 = i;
                }
                else
                {
                    if (x2 > x1)
                    {
                        exAgent.Merge(xstart + x1 * nRow, y1, xstart + x2 * nRow + 1, y2);
                    }
                    x1 = i;
                    str = str1;
                }
            }
        }

        public void BackUp(DataSet db)
        {
            try
            {
                if (!System.IO.Directory.Exists("T:"))
                {
                    return;
                }
                if (!DestFile.Contains("分磅单"))
                    return;
                string tempfile = DestFile.Replace("C:\\IntexReport\\分磅单", "T:\\YGRAB50GB20");
                if (File.Exists(tempfile))
                {
                    throw new Exception(tempfile + "已经存在,禁止重复打印");
                }
                File.Copy(DestFile, tempfile);
                string content = DataToXml.CDataToXml(db);
                DataRow dwLog = VLOG.TBMain.NewRow();
                VLOG.SetUpNewRow(ref dwLog);
                dwLog["ICCTABLENAME"] = "YGRAB50GB20";
                dwLog["CONTENTS"] = content.Substring(0, 3500);
                VLOG.InsertICC_LOG(dwLog);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}